package com.hbnu.exercise.mysql;

import java.io.IOException;
import java.io.InputStream;
import java.net.ConnectException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Properties;

public class DatabaseDemo03
{
    public static void main(String[] args)
    {
        InputStream resourceAsStream = DatabaseDemo03.class.getClassLoader().getResourceAsStream("jdbc.properties");
        Properties properties = new Properties();

        try
        {
            properties.load(resourceAsStream);
            String driverClass = properties.getProperty("driverClass");
            String url = properties.getProperty("url");
            String user = properties.getProperty("user");
            String password = properties.getProperty("password");

            Class.forName(driverClass);

            Connection connection = DriverManager.getConnection(url, user, password);

            Statement statement = connection.createStatement();

            String sql = "SELECT * FROM tb_user";
            ResultSet resultSet = statement.executeQuery(sql);

            while (resultSet.next())
            {
//                int id = resultSet.getInt(1);
//                String username = resultSet.getString(2);
//                String pwd = resultSet.getString(3);
//                String address = resultSet.getString(4);
//                double salary = resultSet.getDouble(5);

                int id = resultSet.getInt("id");
                String username = resultSet.getString("username");
                String pwd = resultSet.getString("password");
                String address = resultSet.getString("address");
                double salary = resultSet.getDouble("salary");
                System.out.println(id + "\t" + username + "\t" + pwd + "\t" + address + "\t" + salary);
            }

            resultSet.close();
            statement.close();
            connection.close();
        } catch (Exception e)
        {
            throw new RuntimeException(e);
        }
    }
}
